#include "progressicongenerator.h"
#include <QDebug>
#include <QFont>
#include <QFontMetrics>
#include <QLinearGradient>
#include <QRadialGradient>
#include <QPainterPath>
#include <QTimer>
#include <QApplication>
// 静态成员变量定义
QHash<QString, QIcon> ProgressIconGenerator::s_iconCache;
QHash<QString, QPixmap> ProgressIconGenerator::s_pixmapCache;
QIcon ProgressIconGenerator::createProgressIcon(int progress, ProgressStyle style, IconSize size)
{
    QString key = generateCacheKey("progress", progress, size);
    key += QString("_%1").arg(static_cast<int>(style));
    if (s_iconCache.contains(key)) {
        return s_iconCache[key];
    }
    QPixmap pixmap;
    switch (style) {
        case ProgressStyle::Pie:
            pixmap = generatePieProgressPixmap(progress, size);
            break;
        case ProgressStyle::Circular:
            pixmap = generateCircularProgressPixmap(progress, size);
            break;
        case ProgressStyle::Bar:
            pixmap = generateBarProgressPixmap(progress, size);
            break;
        case ProgressStyle::Dots:
            pixmap = generateDotsProgressPixmap(progress, size);
            break;
        case ProgressStyle::Spinner:
            pixmap = generateSpinnerPixmap(size);
            break;
    }
    QIcon icon(pixmap);
    s_iconCache[key] = icon;
    return icon;
}
QIcon ProgressIconGenerator::createPieProgressIcon(int progress, IconSize size)
{
    return createProgressIcon(progress, ProgressStyle::Pie, size);
}
QIcon ProgressIconGenerator::createCircularProgressIcon(int progress, IconSize size)
{
    return createProgressIcon(progress, ProgressStyle::Circular, size);
}
QIcon ProgressIconGenerator::createBarProgressIcon(int progress, IconSize size)
{
    return createProgressIcon(progress, ProgressStyle::Bar, size);
}
QIcon ProgressIconGenerator::createDotsProgressIcon(int progress, IconSize size)
{
    return createProgressIcon(progress, ProgressStyle::Dots, size);
}
QIcon ProgressIconGenerator::createSpinnerIcon(IconSize size)
{
    return createProgressIcon(0, ProgressStyle::Spinner, size);
}
QIcon ProgressIconGenerator::createCustomProgressIcon(int progress, const QColor& primaryColor, 
                                                     const QColor& backgroundColor, 
                                                     ProgressStyle style, IconSize size)
{
    QString key = generateCustomCacheKey("custom", progress, primaryColor, backgroundColor, size);
    key += QString("_%1").arg(static_cast<int>(style));
    if (s_iconCache.contains(key)) {
        return s_iconCache[key];
    }
    int sizeInt = static_cast<int>(size);
    QPixmap pixmap(sizeInt, sizeInt);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing);
    QRect rect(0, 0, sizeInt, sizeInt);
    QColor bgColor = backgroundColor.isValid() ? backgroundColor : getDefaultBackgroundColor();
    switch (style) {
        case ProgressStyle::Pie:
            drawPieProgress(painter, rect, progress, primaryColor, bgColor);
            break;
        case ProgressStyle::Circular:
            drawCircularProgress(painter, rect, progress, primaryColor, bgColor);
            break;
        case ProgressStyle::Bar:
            drawBarProgress(painter, rect, progress, primaryColor, bgColor);
            break;
        case ProgressStyle::Dots:
            drawDotsProgress(painter, rect, progress, primaryColor, bgColor);
            break;
        case ProgressStyle::Spinner:
            drawSpinner(painter, rect, primaryColor);
            break;
    }
    QIcon icon(pixmap);
    s_iconCache[key] = icon;
    return icon;
}
void ProgressIconGenerator::clearCache()
{
    s_iconCache.clear();
    s_pixmapCache.clear();
}
int ProgressIconGenerator::getCacheSize()
{
    return s_iconCache.size() + s_pixmapCache.size();
}
QPixmap ProgressIconGenerator::generatePieProgressPixmap(int progress, IconSize size)
{
    int sizeInt = static_cast<int>(size);
    QPixmap pixmap(sizeInt, sizeInt);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing);
    QRect rect(0, 0, sizeInt, sizeInt);
    drawPieProgress(painter, rect, progress, getDefaultPrimaryColor(), getDefaultBackgroundColor());
    return pixmap;
}
QPixmap ProgressIconGenerator::generateCircularProgressPixmap(int progress, IconSize size)
{
    int sizeInt = static_cast<int>(size);
    QPixmap pixmap(sizeInt, sizeInt);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing);
    QRect rect(0, 0, sizeInt, sizeInt);
    drawCircularProgress(painter, rect, progress, getDefaultPrimaryColor(), getDefaultBackgroundColor());
    return pixmap;
}
QPixmap ProgressIconGenerator::generateBarProgressPixmap(int progress, IconSize size)
{
    int sizeInt = static_cast<int>(size);
    QPixmap pixmap(sizeInt, sizeInt);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing);
    QRect rect(0, 0, sizeInt, sizeInt);
    drawBarProgress(painter, rect, progress, getDefaultPrimaryColor(), getDefaultBackgroundColor());
    return pixmap;
}
QPixmap ProgressIconGenerator::generateDotsProgressPixmap(int progress, IconSize size)
{
    int sizeInt = static_cast<int>(size);
    QPixmap pixmap(sizeInt, sizeInt);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing);
    QRect rect(0, 0, sizeInt, sizeInt);
    drawDotsProgress(painter, rect, progress, getDefaultPrimaryColor(), getDefaultBackgroundColor());
    return pixmap;
}
QPixmap ProgressIconGenerator::generateSpinnerPixmap(IconSize size)
{
    int sizeInt = static_cast<int>(size);
    QPixmap pixmap(sizeInt, sizeInt);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing);
    QRect rect(0, 0, sizeInt, sizeInt);
    drawSpinner(painter, rect, getDefaultPrimaryColor());
    return pixmap;
}
void ProgressIconGenerator::drawPieProgress(QPainter& painter, const QRect& rect, int progress, 
                                           const QColor& primaryColor, const QColor& backgroundColor)
{
    // 绘制背景圆
    painter.setBrush(QBrush(backgroundColor));
    painter.setPen(QPen(backgroundColor, 1));
    painter.drawEllipse(rect);
    // 绘制进度饼图
    if (progress > 0) {
        painter.setBrush(QBrush(primaryColor));
        painter.setPen(QPen(primaryColor, 1));
        int startAngle = 90 * 16; // 从顶部开始
        int spanAngle = -progress * 3.6 * 16; // 顺时针绘制
        painter.drawPie(rect, startAngle, spanAngle);
    }
    // 绘制进度文字（大图标时）
    if (rect.width() >= 24) {
        painter.setPen(QPen(Qt::white, 1));
        QFont font = painter.font();
        font.setPointSize(rect.width() / 4);
        font.setBold(true);
        painter.setFont(font);
        QString text = QString("%1%").arg(progress);
        painter.drawText(rect, Qt::AlignCenter, text);
    }
}
void ProgressIconGenerator::drawCircularProgress(QPainter& painter, const QRect& rect, int progress,
                                                const QColor& primaryColor, const QColor& backgroundColor)
{
    int centerX = rect.center().x();
    int centerY = rect.center().y();
    int radius = qMin(rect.width(), rect.height()) / 2 - 2;
    // 绘制背景圆环
    painter.setPen(QPen(backgroundColor, 3));
    painter.drawEllipse(centerX - radius, centerY - radius, radius * 2, radius * 2);
    // 绘制进度圆环
    if (progress > 0) {
        painter.setPen(QPen(primaryColor, 3));
        int startAngle = 90 * 16; // 从顶部开始
        int spanAngle = -progress * 3.6 * 16; // 顺时针绘制
        painter.drawArc(centerX - radius, centerY - radius, radius * 2, radius * 2, startAngle, spanAngle);
    }
    // 绘制进度文字（大图标时）
    if (rect.width() >= 24) {
        painter.setPen(QPen(primaryColor, 1));
        QFont font = painter.font();
        font.setPointSize(rect.width() / 6);
        font.setBold(true);
        painter.setFont(font);
        QString text = QString("%1%").arg(progress);
        painter.drawText(rect, Qt::AlignCenter, text);
    }
}
void ProgressIconGenerator::drawBarProgress(QPainter& painter, const QRect& rect, int progress,
                                           const QColor& primaryColor, const QColor& backgroundColor)
{
    int barHeight = rect.height() / 4;
    int barY = rect.center().y() - barHeight / 2;
    int barWidth = rect.width() - 4;
    int barX = 2;
    // 绘制背景条
    painter.setBrush(QBrush(backgroundColor));
    painter.setPen(QPen(backgroundColor, 1));
    painter.drawRoundedRect(barX, barY, barWidth, barHeight, barHeight/2, barHeight/2);
    // 绘制进度条
    if (progress > 0) {
        int progressWidth = (barWidth * progress) / 100;
        painter.setBrush(QBrush(primaryColor));
        painter.setPen(QPen(primaryColor, 1));
        painter.drawRoundedRect(barX, barY, progressWidth, barHeight, barHeight/2, barHeight/2);
    }
    // 绘制进度文字（大图标时）
    if (rect.width() >= 24) {
        painter.setPen(QPen(primaryColor, 1));
        QFont font = painter.font();
        font.setPointSize(rect.width() / 6);
        font.setBold(true);
        painter.setFont(font);
        QString text = QString("%1%").arg(progress);
        painter.drawText(rect, Qt::AlignCenter, text);
    }
}
void ProgressIconGenerator::drawDotsProgress(QPainter& painter, const QRect& rect, int progress,
                                           const QColor& primaryColor, const QColor& backgroundColor)
{
    int centerX = rect.center().x();
    int centerY = rect.center().y();
    int dotCount = 5;
    int dotRadius = rect.width() / 12;
    int spacing = rect.width() / 6;
    for (int i = 0; i < dotCount; ++i) {
        int x = centerX - (dotCount - 1) * spacing / 2 + i * spacing;
        int y = centerY;
        QColor dotColor = (i * 100 / dotCount < progress) ? primaryColor : backgroundColor;
        painter.setBrush(QBrush(dotColor));
        painter.setPen(QPen(dotColor, 1));
        painter.drawEllipse(x - dotRadius, y - dotRadius, dotRadius * 2, dotRadius * 2);
    }
}
void ProgressIconGenerator::drawSpinner(QPainter& painter, const QRect& rect, const QColor& primaryColor)
{
    int centerX = rect.center().x();
    int centerY = rect.center().y();
    int radius = qMin(rect.width(), rect.height()) / 2 - 2;
    // 绘制旋转的弧线
    painter.setPen(QPen(primaryColor, 3));
    painter.drawArc(centerX - radius, centerY - radius, radius * 2, radius * 2, 0, 270 * 16);
}
QString ProgressIconGenerator::generateCacheKey(const QString& type, int progress, IconSize size)
{
    return QString("%1_%2_%3").arg(type).arg(progress).arg(static_cast<int>(size));
}
QString ProgressIconGenerator::generateCustomCacheKey(const QString& type, int progress, 
                                                      const QColor& primaryColor, 
                                                      const QColor& backgroundColor, IconSize size)
{
    return QString("%1_%2_%3_%4_%5_%6")
        .arg(type)
        .arg(progress)
        .arg(primaryColor.rgba())
        .arg(backgroundColor.rgba())
        .arg(static_cast<int>(size));
}
QColor ProgressIconGenerator::getDefaultPrimaryColor()
{
    return QColor(0, 122, 255); // 系统蓝
}
QColor ProgressIconGenerator::getDefaultBackgroundColor()
{
    return QColor(200, 200, 200); // 浅灰色
}
QColor ProgressIconGenerator::getProgressColor(int progress)
{
    if (progress < 30) {
        return QColor(255, 149, 0); // 橙色
    } else if (progress < 70) {
        return QColor(255, 204, 0); // 黄色
    } else {
        return QColor(52, 199, 89); // 绿色
    }
}
